﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace wlm.wsx.src.wlm.Newwsx
{
    public class Element
    {
        /// <summary>
        /// 数据类型
        /// </summary>
        private string _DataType = string.Empty;

        /// <summary>
        /// 变量名称
        /// </summary>
        private string _Name = string.Empty;

        /// <summary>
        /// 变量值
        /// </summary>
        private string _Value = string.Empty;

        /// <summary>
        /// 变量xml节点
        /// </summary>
        private XmlNode _XmlNode = null;

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType
        {
            get { return this._DataType.ToUpper(); }
            set { this._DataType = value; }
        }

        /// <summary>
        /// 变量名称
        /// </summary>
        public string Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }
        /// <summary>
        /// 变量值
        /// </summary>
        public string Value
        {
            get { return this._Value; }
            set { this._Value = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public XmlNode Node
        {
            get { return this._XmlNode; }
        }

        /// <summary>
        /// 设置节点数据
        /// </summary>
        /// <param name="doc"></param>
        public void SetXmlNode(XmlDocument doc)
        {
            if (doc == null)
                return;

            this._XmlNode = doc.CreateElement(this._DataType);
            XmlAttribute nameAttribute = doc.CreateAttribute("name");
            nameAttribute.InnerText = this._Name;
            this._XmlNode.Attributes.Append(nameAttribute);
            this._XmlNode.InnerText = this._Value;
        }

        /// <summary>
        /// 输出为xml格式，<DataType name = "Name">Value</DataType>>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sbResult = new StringBuilder();
            sbResult.Append(string.Format("<{0}", this._DataType));
            if (!string.IsNullOrEmpty(this._Name))
                sbResult.Append(string.Format(" name=\"{0}\">", this._Name));
            else
                sbResult.Append(">");
            sbResult.Append(this._Value);
            sbResult.Append(string.Format("</{0}>", this._DataType));
            return sbResult.ToString();
        }

    }
}
